﻿using app.Common;
using eService.BLL;
using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Printing;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
using System.Xml;

namespace eService.Presentation
{
    public partial class usrcCauHinh_DuLieu : UserControl
    {
        private XmlDocument xmlDoc = new XmlDocument();
        public bool showPublic = false;

        public usrcCauHinh_DuLieu()
        {
            InitializeComponent();
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");
            LoadThongSo();
        }
        private void LoadThongSo()
        {
            try
            {
                string sValue = "", sServer = "", sDatabase = "", sPass = "", sUserName = "", sSaoLuu = "", sSoFileLuu = "";
                sValue = AppSetting.getSetting().getConfigValue("ip_server");
                sServer = sValue;

                sValue = AppSetting.getSetting().getConfigValue("database_server");
                sDatabase = sValue;

                sValue = AppSetting.getSetting().getConfigValue("user_server");
                sUserName = sValue;

                sValue = AppSetting.getSetting().getConfigValue("password_server");
                sPass = TCEncryption.ToDecrypt(sValue);

                sValue = AppSetting.getSetting().getConfigValue("Autobackup");
                sSaoLuu = sValue;

                sValue = AppSetting.getSetting().getConfigValue("BackupCount");
                sSoFileLuu = sValue;

                if (sServer != "" && sUserName != "" && sPass != "" && sDatabase != "")
                {
                    txtMayChu.Text = sServer;
                    txtDuLieu.Text = sDatabase;
                    txtTaiKhoan.Text = sUserName;
                    txtMatKhau.Text = sPass;
                    chbSaoLuuTuDong.Checked = bool.Parse(sSaoLuu);
                    numSoFileLuu.Value = int.Parse(sSoFileLuu);

                    bool KQ = false;
                    KQ = ConnectionProvider.LoadAllConnection();
                    if (!KQ) MessageBox.Show("Thông tin cấu hình không chính xác!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    MessageBox.Show("Vui lòng điền đầy đủ thông tin!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            catch
            {
            }
        }
        private void LuuThongSo()
        {
            try
            {
                UpdateKey("ip_server", txtMayChu.Text);
                UpdateKey("database_server", txtDuLieu.Text);
                UpdateKey("user_server", txtTaiKhoan.Text);
                UpdateKey("password_server", TCEncryption.ToEncrypt(txtMatKhau.Text));
                UpdateKey("Autobackup", chbSaoLuuTuDong.Checked.ToString());
                UpdateKey("BackupCount", numSoFileLuu.Value.ToString());
                ConfigurationManager.RefreshSection("appSettings");

                bool KQ = false;
                KQ = ConnectionProvider.LoadConnection(txtMayChu.Text,txtDuLieu.Text,txtTaiKhoan.Text,txtMatKhau.Text);
                if (KQ)
                {
                    string sServer = "", sDatabase = "", sPass = "", sUserName = "", sSaoLuu = "", sSoFileLuu = "";
                    sServer = txtMayChu.Text;
                    sDatabase = txtDuLieu.Text;
                    sUserName = txtTaiKhoan.Text;
                    sPass = txtMatKhau.Text;
                    sSaoLuu = chbSaoLuuTuDong.Checked.ToString();
                    sSoFileLuu = numSoFileLuu.Value.ToString();

                    if (sServer != "" && sUserName != "" && sPass != "" && sDatabase != "")
                    {
                        AppSetting.getSetting().setConfigValue("ip_server", sServer);
                        AppSetting.getSetting().setConfigValue("user_server", sUserName);
                        AppSetting.getSetting().setConfigValue("password_server", TCEncryption.ToEncrypt(sPass));
                        AppSetting.getSetting().setConfigValue("database_server", sDatabase);
                        AppSetting.getSetting().setConfigValue("Autobackup", sSaoLuu);
                        AppSetting.getSetting().setConfigValue("BackupCount", sSoFileLuu);
                    }
                    else
                    {
                        MessageBox.Show("Vui lòng điền đầy đủ thông tin!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    MessageBox.Show("Kết nối tới máy chủ thành công.\nHệ thống sẽ khởi động lại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    Application.Restart();
                }
                else MessageBox.Show("Kết nối tới máy chủ thất bại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void btnXacNhan_Click(object sender, EventArgs e)
        {
            LuuThongSo();
        }
        public void UpdateKey(string strKey, string newValue)
        {
            this.xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "eService.Presentation.exe.config");
            if (!this.KeyExists(strKey))
            {
                this.CreateKey(strKey);
            }
            XmlNode xmlNode = this.xmlDoc.SelectSingleNode("configuration/appSettings");
            foreach (XmlNode xmlNode2 in xmlNode)
            {
                if (xmlNode2.Attributes["key"].Value == strKey)
                {
                    xmlNode2.Attributes["value"].Value = newValue;
                }
            }
            this.xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "eService.Presentation.exe.config");
            this.xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        }
        public bool KeyExists(string strKey)
        {
            this.xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "eService.Presentation.exe.config");
            XmlNode xmlNode = this.xmlDoc.SelectSingleNode("configuration/appSettings");
            bool result;
            foreach (XmlNode xmlNode2 in xmlNode)
            {
                if (xmlNode2.Attributes["key"].Value == strKey)
                {
                    result = true;
                    return result;
                }
            }
            result = false;
            return result;
        }
        private void CreateKey(string strKey)
        {
            try
            {
                this.xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "eService.Presentation.exe.config");
                XmlNode xmlNode = this.xmlDoc.SelectSingleNode("configuration/appSettings");
                XmlNode xmlNode2;
                if (xmlNode.FirstChild != null)
                {
                    xmlNode2 = xmlNode.FirstChild.Clone();
                }
                else
                {
                    xmlNode2 = this.xmlDoc.CreateElement("add");
                    XmlAttribute node = this.xmlDoc.CreateAttribute("key");
                    XmlAttribute node2 = this.xmlDoc.CreateAttribute("value");
                    xmlNode2.Attributes.Append(node);
                    xmlNode2.Attributes.Append(node2);
                    xmlNode.AppendChild(xmlNode2);
                }
                xmlNode2.Attributes["key"].Value = strKey;
                xmlNode2.Attributes["value"].Value = "";
                xmlNode.AppendChild(xmlNode2);
                this.xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "eService.Presentation.exe.config");
            }
            catch
            {
            }
        }
    }
}
